package com.ihk.test;
import java.util.Properties;

import com.alibaba.fastjson.JSON;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
/**
 * Created by Administrator on 2017/2/21.
 */
public class MyProducer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("metadata.broker.list","kafka1:9092,kafka2:9092,kafka3:9092");
        props.setProperty("serializer.class","kafka.serializer.StringEncoder");
        props.put("request.required.acks","1");
        ProducerConfig config = new ProducerConfig(props);
        //创建生产这对象
        Producer<String, String> producer = new Producer<String, String>(config);
        //生成消息
        KeyedMessage<String, String> data1 = new KeyedMessage<String, String>("top1", "dfgdfgdfg");
        KeyedMessage<String, String> data2 = new KeyedMessage<String, String>("top2","hello world");
        try {
            int i =1;
            while(true){//i < 100
                //发送消息
                System.out.println(data1+"----"+data2);
                producer.send(data1);
                producer.send(data2);
                i++;
//                Thread.sleep(500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.close();
    }
}
